package com.fr.report.io;

import com.fr.base.FRContext;
import com.fr.base.FRFont;
import com.fr.base.Style;
import com.fr.base.background.ColorBackground;
import com.fr.report.CellElement;
import com.fr.report.DefaultCellElement;
import com.fr.report.Margin;
import com.fr.report.RWorkBook;
import com.fr.report.RWorkSheet;
import com.fr.report.Report;
import com.fr.report.ReportSettings;
import com.fr.report.ResultReport;
import com.fr.report.ResultWorkBook;
import com.fr.report.TableDataSource;
import com.fr.report.TemplateWorkBook;
import com.fr.report.WorkBook;
import com.fr.report.WorkSheet;
import com.fr.report.cellElement.TextFormat;
import com.fr.report.core.FormReport;
import com.fr.report.core.ReportUtils;
import com.fr.report.io.core.ExcelUtils;
import com.fr.report.web.ui.Widget;
import com.fr.report.write.ValueVerifier;
import com.fr.third.org.apache.poi.hssf.usermodel.HSSFCell;
import com.fr.third.org.apache.poi.hssf.usermodel.HSSFCellStyle;
import com.fr.third.org.apache.poi.hssf.usermodel.HSSFDataFormat;
import com.fr.third.org.apache.poi.hssf.usermodel.HSSFFont;
import com.fr.third.org.apache.poi.hssf.usermodel.HSSFPalette;
import com.fr.third.org.apache.poi.hssf.usermodel.HSSFPrintSetup;
import com.fr.third.org.apache.poi.hssf.usermodel.HSSFRow;
import com.fr.third.org.apache.poi.hssf.usermodel.HSSFSheet;
import com.fr.third.org.apache.poi.hssf.usermodel.HSSFWorkbook;
import com.fr.third.org.apache.poi.hssf.util.HSSFColor;
import com.fr.third.org.apache.poi.hssf.util.HSSFColor.AUTOMATIC;
import com.fr.third.org.apache.poi.hssf.util.Region;
import com.fr.third.org.apache.poi.poifs.filesystem.POIFSFileSystem;
import com.fr.web.core.WebUtils;
import java.awt.Color;
import java.io.InputStream;
import java.io.PrintStream;
import java.text.DecimalFormat;
import java.text.Format;
import java.util.Iterator;
import java.util.Locale;
import java.util.Map;

public class ExcelImporter extends AbstractImporter
{
  public RWorkBook generateResult(InputStream paramInputStream)
    throws Exception
  {
    return ((RWorkBook)generate(paramInputStream, new ResultBookFactory(null)));
  }

  public WorkBook generateTemplate(InputStream paramInputStream)
    throws Exception
  {
    return ((WorkBook)generate(paramInputStream, new TemplateBookFactory(null)));
  }

  private com.fr.report.FineBook generate(InputStream paramInputStream, BookFactory paramBookFactory)
    throws Exception
  {
    POIFSFileSystem localPOIFSFileSystem = new POIFSFileSystem(paramInputStream);
    HSSFWorkbook localHSSFWorkbook = new HSSFWorkbook(localPOIFSFileSystem);
    com.fr.report.FineBook localFineBook = paramBookFactory.getBookInstance();
    int i = localHSSFWorkbook.getNumberOfSheets();
    for (int j = 0; j < i; ++j)
    {
      HSSFSheet localHSSFSheet = localHSSFWorkbook.getSheetAt(j);
      Report localReport = paramBookFactory.getChildReportInstance(localHSSFWorkbook.getSheetName(j));
      ReportSettings localReportSettings = localReport.getReportSettings();
      HSSFPrintSetup localHSSFPrintSetup = localHSSFSheet.getPrintSetup();
      localReportSettings.setHeaderHeight(localHSSFPrintSetup.getHeaderMargin());
      localReportSettings.setFooterHeight(localHSSFPrintSetup.getFooterMargin());
      if (localHSSFPrintSetup.getLandscape())
        localReportSettings.setOrientation(1);
      else
        localReportSettings.setOrientation(0);
      if (localHSSFPrintSetup.getLeftToRight())
        localReportSettings.setPageOrder(1);
      else
        localReportSettings.setPageOrder(0);
      Margin localMargin = localReportSettings.getMargin();
      localMargin.setTop(localHSSFSheet.getMargin(2));
      localMargin.setLeft(localHSSFSheet.getMargin(0));
      localMargin.setBottom(localHSSFSheet.getMargin(3));
      localMargin.setRight(localHSSFSheet.getMargin(1));
      int k = localHSSFSheet.getLastRowNum();
      for (int l = localHSSFSheet.getFirstRowNum(); l <= k; ++l)
      {
        HSSFRow localHSSFRow = localHSSFSheet.getRow(l);
        if (localHSSFRow == null)
          break label442:
        localReport.setRowHeight(l, (int)(localHSSFRow.getHeight() / 15.207675329298683D));
        int i1 = localHSSFRow.getLastCellNum();
        for (int i2 = Math.max(0, localHSSFRow.getFirstCellNum()); i2 <= i1; ++i2)
        {
          if (localHSSFSheet.getColumnWidth((short)i2) != 8)
            localReport.setColumnWidth(i2, (int)(localHSSFSheet.getColumnWidth((short)i2) / 34.741999999999997D));
          HSSFCell localHSSFCell = localHSSFRow.getCell((short)i2);
          if (localHSSFCell == null)
            break label436:
          if (isHiddenMergedCell(localHSSFSheet, i2, l))
            break label436:
          int[] arrayOfInt = getMergetColumnRow(localHSSFSheet, i2, l);
          int i3 = 1;
          int i4 = 1;
          if (arrayOfInt != null)
          {
            i3 = arrayOfInt[0];
            i4 = arrayOfInt[1];
          }
          DefaultCellElement localDefaultCellElement = new DefaultCellElement(i2, l, i3, i4, ExcelUtils.getHSSFCellValue(localHSSFCell));
          localReport.addCellElement(localDefaultCellElement, false);
          localDefaultCellElement.setStyle(createStyle(localHSSFWorkbook, localHSSFCell));
        }
      }
    }
    label436: label442: paramInputStream.close();
    return localFineBook;
  }

  private boolean isHiddenMergedCell(HSSFSheet paramHSSFSheet, int paramInt1, int paramInt2)
  {
    for (int i = 0; i < paramHSSFSheet.getNumMergedRegions(); ++i)
    {
      Region localRegion = paramHSSFSheet.getMergedRegionAt(i);
      if ((localRegion.getColumnFrom() <= paramInt1) && (localRegion.getColumnTo() >= paramInt1) && (localRegion.getRowFrom() <= paramInt2) && (localRegion.getRowTo() >= paramInt2))
        return ((localRegion.getColumnFrom() != paramInt1) || (localRegion.getRowFrom() != paramInt2));
    }
    return false;
  }

  private int[] getMergetColumnRow(HSSFSheet paramHSSFSheet, int paramInt1, int paramInt2)
  {
    for (int i = 0; i < paramHSSFSheet.getNumMergedRegions(); ++i)
    {
      Region localRegion = paramHSSFSheet.getMergedRegionAt(i);
      if ((localRegion.getColumnFrom() <= paramInt1) && (localRegion.getColumnTo() >= paramInt1) && (localRegion.getRowFrom() <= paramInt2) && (localRegion.getRowTo() >= paramInt2))
        return new int[] { localRegion.getColumnTo() - localRegion.getColumnFrom() + 1, localRegion.getRowTo() - localRegion.getRowFrom() + 1 };
    }
    return null;
  }

  private Style createStyle(HSSFWorkbook paramHSSFWorkbook, HSSFCell paramHSSFCell)
  {
    Style localStyle = Style.DEFAULT_STYLE;
    HSSFCellStyle localHSSFCellStyle = paramHSSFCell.getCellStyle();
    try
    {
      short s = localHSSFCellStyle.getDataFormat();
      String str = paramHSSFWorkbook.createDataFormat().getFormat(s);
      if (str != null)
      {
        Object localObject1;
        if (str.equals("@"))
        {
          localObject1 = TextFormat.getInstance();
        }
        else if (str.toUpperCase().equals("GENERAL"))
        {
          localObject1 = null;
        }
        else
        {
          str = str.trim();
          if (str.endsWith("_"))
          {
            str = str.substring(0, str.length() - 1);
          }
          else
          {
            k = str.indexOf("_);");
            if (k != -1)
              str = str.substring(0, k);
            if (str.startsWith("_(*"))
              str = str.substring(3);
          }
          if (str.indexOf("#,##0_") != -1)
            str = "#,##0";
          if (str.indexOf("#,##0.00") != -1)
            str = "#,##0.00";
          int k = -1;
          if ((k = str.indexOf("+")) != -1)
            str = str.substring(0, k) + str.substring(k + 1);
          localObject2 = new DecimalFormat();
          ((DecimalFormat)localObject2).applyPattern(str);
          localObject1 = localObject2;
        }
        localStyle = localStyle.deriveFormat((Format)localObject1);
      }
    }
    catch (Exception localException)
    {
      System.err.println("Excel format pattern not supported by java: ");
      localException.printStackTrace();
    }
    HSSFFont localHSSFFont = paramHSSFWorkbook.getFontAt(localHSSFCellStyle.getFontIndex());
    int i = 0;
    if (localHSSFFont.getBoldweight() == 700)
      ++i;
    if (localHSSFFont.getItalic())
      i += 2;
    int j = localHSSFFont.getFontHeightInPoints() * 4 / 3;
    FRFont localFRFont = FRFont.getInstance(localHSSFFont.getFontName(), i, j);
    if ((FRContext.getLocale().equals(Locale.CHINA)) && (!(localFRFont.canDisplay(23435))))
      localFRFont = FRFont.getInstance("宋体", i, j);
    Object localObject2 = createColor(paramHSSFWorkbook, localHSSFFont.getColor());
    if (localObject2 != null)
      localFRFont = localFRFont.applyForeground((Color)localObject2);
    if (localHSSFFont.getStrikeout())
      localFRFont = localFRFont.applyStrikethrough(true);
    if (localHSSFFont.getUnderline() == 0)
      localFRFont = localFRFont.applyUnderline(0);
    else if ((localHSSFFont.getUnderline() == 1) || (localHSSFFont.getUnderline() == 33))
      localFRFont = localFRFont.applyUnderline(1);
    else if ((localHSSFFont.getUnderline() == 2) || (localHSSFFont.getUnderline() == 34))
      localFRFont = localFRFont.applyUnderline(6);
    localStyle = localStyle.deriveFRFont(localFRFont);
    Color localColor1 = createColor(paramHSSFWorkbook, localHSSFCellStyle.getFillForegroundColor());
    if (localColor1 != null)
      localStyle = localStyle.deriveBackground(ColorBackground.getInstance(localColor1));
    int l = localHSSFCellStyle.getAlignment();
    if (l == 2)
      localStyle = localStyle.deriveHorizontalAlignment(0);
    else if (l == 3)
      localStyle = localStyle.deriveHorizontalAlignment(4);
    else
      localStyle = localStyle.deriveHorizontalAlignment(2);
    int i1 = localHSSFCellStyle.getVerticalAlignment();
    if (i1 == 0)
      localStyle = localStyle.deriveVerticalAlignment(1);
    else if (i1 == 2)
      localStyle = localStyle.deriveVerticalAlignment(3);
    else
      localStyle = localStyle.deriveVerticalAlignment(0);
    if (localHSSFCellStyle.getWrapText())
      localStyle = localStyle.deriveTextStyle(0);
    else
      localStyle = localStyle.deriveTextStyle(1);
    localStyle = localStyle.deriveRotation(localHSSFCellStyle.getRotation());
    Color localColor2 = createColor(paramHSSFWorkbook, localHSSFCellStyle.getTopBorderColor());
    Color localColor3 = createColor(paramHSSFWorkbook, localHSSFCellStyle.getLeftBorderColor());
    Color localColor4 = createColor(paramHSSFWorkbook, localHSSFCellStyle.getBottomBorderColor());
    Color localColor5 = createColor(paramHSSFWorkbook, localHSSFCellStyle.getRightBorderColor());
    localStyle = localStyle.deriveBorder(getBorderStyle(localHSSFCellStyle.getBorderTop()), (localColor2 == null) ? localStyle.getBorderTopColor() : localColor2, getBorderStyle(localHSSFCellStyle.getBorderBottom()), (localColor4 == null) ? localStyle.getBorderBottomColor() : localColor4, getBorderStyle(localHSSFCellStyle.getBorderLeft()), (localColor3 == null) ? localStyle.getBorderLeftColor() : localColor3, getBorderStyle(localHSSFCellStyle.getBorderRight()), (localColor5 == null) ? localStyle.getBorderRightColor() : localColor5);
    return ((Style)(Style)localStyle);
  }

  private Color createColor(HSSFWorkbook paramHSSFWorkbook, short paramShort)
  {
    HSSFColor localHSSFColor = paramHSSFWorkbook.getCustomPalette().getColor(paramShort);
    if (localHSSFColor == null)
      return null;
    if (localHSSFColor instanceof HSSFColor.AUTOMATIC)
      return null;
    short[] arrayOfShort = localHSSFColor.getTriplet();
    return new Color(arrayOfShort[0], arrayOfShort[1], arrayOfShort[2]);
  }

  private int getBorderStyle(short paramShort)
  {
    switch (paramShort)
    {
    case 1:
      return 1;
    case 2:
      return 2;
    case 3:
      return 3;
    case 4:
      return 4;
    case 5:
      return 5;
    case 6:
      return 6;
    case 7:
      return 7;
    case 8:
      return 8;
    case 9:
      return 9;
    case 10:
      return 10;
    case 11:
      return 11;
    case 12:
      return 12;
    case 13:
      return 13;
    }
    return 0;
  }

  public static boolean importExcel(TemplateWorkBook paramTemplateWorkBook, Map paramMap, InputStream paramInputStream)
  {
    ResultWorkBook localResultWorkBook;
    try
    {
      localResultWorkBook = paramTemplateWorkBook.execute4Write(paramMap);
      for (int i = 0; i < paramTemplateWorkBook.getReportCount(); ++i)
        localResultWorkBook.setReportName(i, paramTemplateWorkBook.getReportName(i));
      importExcel2Book(localResultWorkBook, paramTemplateWorkBook, paramInputStream, paramMap);
      i = 0;
      int j = localResultWorkBook.getReportCount();
      while (i < j)
      {
        ValueVerifier[] arrayOfValueVerifier;
        int k;
        Report localReport = localResultWorkBook.getReport(i);
        if (localReport instanceof FormReport)
        {
          FormReport localFormReport = (FormReport)localReport;
          arrayOfValueVerifier = localFormReport.checkValueVerifiers();
          for (k = 0; k < arrayOfValueVerifier.length; ++k)
          {
            ValueVerifier localValueVerifier = arrayOfValueVerifier[k];
            int l = localValueVerifier.getType();
            if (l != 0)
              return false;
          }
        }
        ++i;
      }
      ReportUtils.submit(localResultWorkBook, paramTemplateWorkBook, WebUtils.dealWithReportParameters(paramTemplateWorkBook, paramMap));
    }
    catch (Exception localException)
    {
      return false;
    }
    return true;
  }

  public static void importExcel2Book(ResultWorkBook paramResultWorkBook, TableDataSource paramTableDataSource, InputStream paramInputStream, Map paramMap)
    throws Exception
  {
    int j;
    WorkBook localWorkBook = new ExcelImporter().generateTemplate(paramInputStream);
    if (localWorkBook != null)
    {
      int i = 0;
      if ((localWorkBook != null) && (paramResultWorkBook != null))
      {
        i = Math.min(localWorkBook.getReportCount(), paramResultWorkBook.getReportCount());
        for (j = 0; j < i; ++j)
        {
          Report localReport = localWorkBook.getReport(j);
          ResultReport localResultReport = paramResultWorkBook.getResultReport(j);
          if (localReport != null)
          {
            if (localResultReport == null)
              break label185:
            Iterator localIterator = localResultReport.cellIterator();
            while (true)
            {
              CellElement localCellElement;
              Object localObject;
              do
              {
                Widget localWidget;
                do
                {
                  if (!(localIterator.hasNext()))
                    break label177;
                  localCellElement = (CellElement)localIterator.next();
                  localWidget = localCellElement.getWidget();
                }
                while (localWidget == null);
                localObject = localReport.getCellValue(localCellElement.getColumn(), localCellElement.getRow());
              }
              while (localObject == null);
              localCellElement.setValue(localObject);
            }
            localResultReport.recalculate(paramMap);
          }
        }
      }
    }
    label177: label185: return;
  }

  private static class ResultBookFactory
    implements ExcelImporter.BookFactory
  {
    private ResultWorkBook book;

    private ResultBookFactory()
    {
      this.book = new RWorkBook();
    }

    public com.fr.report.FineBook getBookInstance()
    {
      return this.book;
    }

    public Report getChildReportInstance(String paramString)
    {
      RWorkSheet localRWorkSheet = new RWorkSheet();
      this.book.addReport(paramString, localRWorkSheet);
      return localRWorkSheet;
    }

    ResultBookFactory(ExcelImporter.1 param1)
    {
    }
  }

  private static class TemplateBookFactory
    implements ExcelImporter.BookFactory
  {
    private TemplateWorkBook book;

    private TemplateBookFactory()
    {
      this.book = new WorkBook();
    }

    public com.fr.report.FineBook getBookInstance()
    {
      return this.book;
    }

    public Report getChildReportInstance(String paramString)
    {
      WorkSheet localWorkSheet = new WorkSheet();
      this.book.addReport(paramString, localWorkSheet);
      return localWorkSheet;
    }

    TemplateBookFactory(ExcelImporter.1 param1)
    {
    }
  }

  private static abstract interface BookFactory
  {
    public abstract com.fr.report.FineBook getBookInstance();

    public abstract Report getChildReportInstance(String paramString);
  }
}